package com.yf.course.job;

import com.alibaba.fastjson.JSON;
import com.yf.base.enums.OpenType;
import com.yf.base.utils.DateUtils;
import com.yf.course.modules.course.dto.request.CourseNotifyReqDTO;
import com.yf.course.modules.course.entity.Course;
import com.yf.course.modules.course.service.CourseDepartService;
import com.yf.course.modules.course.service.CoursePersonService;
import com.yf.course.modules.course.service.CourseService;
import com.yf.job.service.JobService;
import com.yf.notify.enums.MsgId;
import com.yf.system.modules.user.dto.request.UserMsgReqDTO;
import com.yf.system.modules.user.service.UserMsgService;
import lombok.extern.log4j.Log4j2;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 考试参与通知
 * @author bool
 */
@Log4j2
@Component
public class CourseNotifyJob implements Job {

    @Autowired
    private CourseService courseService;

    @Autowired
    private CoursePersonService coursePersonService;

    @Autowired
    private CourseDepartService courseDepartService;


    @Autowired
    private UserMsgService userMsgService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {


        JobDetail detail = jobExecutionContext.getJobDetail();
        String name = detail.getKey().getName();
        String group = detail.getKey().getGroup();
        String data = String.valueOf(detail.getJobDataMap().get(JobService.TASK_DATA));

        log.info("++++++++++定时任务：发送学习提醒");
        log.info("++++++++++jobName:{}", name);
        log.info("++++++++++jobGroup:{}", group);
        log.info("++++++++++taskData:{}", data);


        CourseNotifyReqDTO dto = JSON.parseObject(data, CourseNotifyReqDTO.class);

        Course course = courseService.getById(dto.getCourseId());
        if(course == null){
            return;
        }

        List<String> userIds = new ArrayList<>();

        // 部门开放
        if(OpenType.DEPART.equals(course.getOpenType())){
            userIds = courseDepartService.listUserIdByCourse(dto.getCourseId(), dto.getScope());
        }

        // 指定人员
        if(OpenType.PERSON.equals(course.getOpenType())){
            userIds = coursePersonService.listUserIdByCourse(dto.getCourseId(), dto.getScope());
        }

        // 发信息
        if(!CollectionUtils.isEmpty(userIds)){

            UserMsgReqDTO sendDTO = new UserMsgReqDTO();

            // 3种发送方式
            sendDTO.setUseIm(dto.getUseIm());
            sendDTO.setUseSms(dto.getUseSms());
            sendDTO.setUseEmail(dto.getUseEmail());
            sendDTO.setTmplId(MsgId.COURSE_PREPARE);
            sendDTO.setUserIds(userIds);

            // 必须按顺序
            LinkedHashMap<String,String> params = new LinkedHashMap<>();
            params.put("title", course.getTitle());
            params.put("date", DateUtils.formatDate(course.getStartTime(), "yyyy-MM-dd HH:mm"));
            sendDTO.setParams(params);
            userMsgService.sendMsg(sendDTO);
        }

    }



}